package se19team1e.gui.awt;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.FlowLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import se19team1e.controller.Application;
import se19team1e.controller.UpdateJobAdvertController;
import se19team1e.entity.CompanyDTO;
import se19team1e.entity.DepartmentDTO;
import se19team1e.entity.JobAdvertDTO;
import se19team1e.exception.DataAccessException;
import se19team1e.gui.MessageBoxHelper;
import se19team1e.gui.UpdateJobAdvertForm;

/**
 * 
 * A GUI form that allows the advertiser to input and update new job adverts. 
 * 
 * @author Prahlada Rao
 *
 */
@SuppressWarnings("serial")
public class AWTUpdateJobAdvertForm extends AWTBaseScreen implements UpdateJobAdvertForm {

    /**
     * A reference to the current owning controller of this form.
     */
    private UpdateJobAdvertController controller;

    /**
     * The job advert input panel.
     */
    private final JobAdvertInputPanel jobAdvertInputPanel;
    
    /**
     * An awt button to submit login credentials.
     */
    private final Button submitButton;

    /**
     * An awt button to cancel job advert.
     */
    private final Button cancelButton;

    /**
     * Constructs a create job advert form, initializing with the application instance.
     * @param application Reference to the current application instance.
     */
    @SuppressWarnings("deprecation")
    public AWTUpdateJobAdvertForm( Application application ) {
        super(application);

        //this.setBounds(150, 50, 575, 659 );  
        // initialize layout
        getContentPane().setLayout( new BorderLayout() );
        
        // create panel to hold all sub panels
        Panel mainPanel = new Panel();
        mainPanel.setLayout(new BorderLayout());
        
        // create panel to hold text field
        this.jobAdvertInputPanel = new JobAdvertInputPanel();
        
        // add panel to center panel
        mainPanel.add( this.jobAdvertInputPanel,BorderLayout.CENTER );
        
        // create panel to hold buttons
        Panel buttonPanel = new Panel();

        // add panel to center panel
        mainPanel.add( buttonPanel, BorderLayout.SOUTH );
        
        // create and add submit button
        this.submitButton = new Button( "Submit" );
        // add callback for submit button
        this.submitButton.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent e ) {
                try {
					updateJobAdvert();
				} catch (DataAccessException e1) {
					e1.printStackTrace();
				}
            }
        });

        // create and add cancel button
        this.cancelButton = new Button( "Cancel" );
        // add callback for cancel button
        this.cancelButton.addActionListener( new ActionListener() {
            @Override
            public void actionPerformed( ActionEvent e ) {
                try {
                    showJobAdvertSearchForm();
                } catch (DataAccessException e1) {
                    e1.printStackTrace();
                }
            }
        });
        
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
        buttonPanel.add( this.submitButton );
        buttonPanel.add( this.cancelButton );
        
        // add panel to frame
        getContentPane().add( mainPanel);
        
        // pack the layout
        this.pack();
        
        // hide the frame
        this.hide();
    }

    /* (non-Javadoc)
     * @see se19team1e.IBaseScreen#updateAvailableActions()
     */
    @Override
    public void updateAvailableActions() {
        this.updateAvailableMenuActions();

        // ensure submit button is enabled
        submitButton.setEnabled( true );
    }

    /* (non-Javadoc)
     * @see se19team1e.gui.CreateJobAdvertForm#initialize(se19team1e.controller.CreateJobAdvertController, java.util.List, java.util.List)
     */
    @Override
    public void initialize(JobAdvertDTO jobAdvertDTO, UpdateJobAdvertController controller, List<CompanyDTO> companies, List<DepartmentDTO> departments ) {
        this.controller = controller;
        
        // initialize the form
        this.jobAdvertInputPanel.initialize(jobAdvertDTO, companies, departments );
    }

    /**
     * Triggers a creation of the current entered job advert info.
     * @throws DataAccessException 
     */
    public void updateJobAdvert() throws DataAccessException {
        // validate first
        if ( !this.validateInput() ) {
            // failure, return
            return;
        }
        
        // delegate to controller
        JobAdvertDTO jobAdvert=new JobAdvertDTO();
        if (getApplication().getCurrentAdvertiser()!=null){
        	jobAdvert.setAdvertiserId(getApplication().getCurrentAdvertiser().getId());
        }
        this.controller.updateJobAdvert( this.jobAdvertInputPanel.newJobAdvertDTOFromInputForUpdate(jobAdvert));
    }

    /**
     * Validates the job advert input to see if it is valid for creation.
     * 
     * <p>
     * Delegates to the login input panel.
     * </p>
     * @return True if valid.
     */
    private boolean validateInput() {
        return this.jobAdvertInputPanel.validateInput( new MessageBoxHelper( this.getApplication() ) );
    }

    /**
     * Triggers a showing of the job advert search form again.
     */
    public void showJobAdvertSearchForm() throws DataAccessException {
        // delegate to controller
        this.controller.showSearchJobAdvertResultsScreen();
    }

}
